package com.ciwong.epaper.modules.reciteWords.dbreciteword;


import com.ciwong.epaper.common.db.DBAsyn;
import com.ciwong.epaper.common.db.DBCallBack;
import com.ciwong.epaper.modules.evaluate.bean.WordDetail;
import com.ciwong.libs.utils.CWLog;

import java.util.List;

public class DBManagerAsyn extends BaseDBManager {
    private String TAG = "DBManagerAsyn";
    private static DBManagerAsyn dbManager = null;

    private DBManagerAsyn() {
        super();
    }

    public static DBManagerAsyn getInstance() {
        if (null == dbManager)
            dbManager = new DBManagerAsyn();
        return dbManager;
    }

    /**
     * 通过did 选出四个选项
     *
     * @param versionId
     * @param dId
     * @return
     */
    public void executeGetOptionsAsyn(final String versionId, final String dId, final DBCallBack dbCallBack) {
        new DBAsyn<List<WordDetail>>() {
            @Override
            protected List<WordDetail> doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                return executeGetOptions(versionId, dId);
            }

            @Override
            protected void onPostExecute(List<WordDetail> result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();
    }

    /**
     * 检测单词书的表是否真的存在，并且内容是否为空
     *
     * @param versionId flag ： 0 表示表不存在，表中数据不存在； 1表示数据不存在，表存在； 2表示表中数据和表都存在；
     */
    public void executeCheckTableAndData(final String versionId, final DBCallBack dbCallBack) {
        new DBAsyn<Integer>() {
            @Override
            protected Integer doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                int flag = 0;//0 表示表不存在，表中数据不存在； 1表示数据不存在，表存在； 2表示表中数据和表都存在；
                if (checkTableIsExist(versionId)) {
                    flag = 1;
                    if (checkTableHasData(versionId)) {
                        flag = 2;
                    }
                }
                return flag;
            }

            @Override
            protected void onPostExecute(Integer result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();
    }

    /**
     * 获取did对应数据(同步)
     *
     * @param versionId
     * @param dId
     * @return
     */
    public void queryDIdWordDetailAsyn(final String versionId, final String dId, final DBCallBack dbCallBack) {
        new DBAsyn<WordDetail>() {
            @Override
            protected WordDetail doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                return executeGetByDId(versionId, dId);
            }

            @Override
            protected void onPostExecute(WordDetail result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();

    }

    /**
     * 操作数据库插入数据(异步)
     *
     * @param versionId
     * @param wordDetails
     */
    public void addAsyn(String versionId, List<WordDetail> wordDetails) {
        addAsyn(versionId, wordDetails, null);
    }

    /**
     * 操作数据库插入数据(异步)
     *
     * @param versionId
     * @param wordDetails
     * @param dbCallBack
     */
    public void addAsyn(final String versionId, final List<WordDetail> wordDetails, final DBCallBack dbCallBack) {
        new DBAsyn<Boolean>() {
            @Override
            protected Boolean doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                try {
                    return executeAddData(versionId, wordDetails, dbCallBack);
                } catch (Exception e) {
                    return false;
                }
            }

            @Override
            protected void onPostExecute(Boolean result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();


    }

    /**
     * 操作数据库插入数据,当数据存在时不插入(异步)
     *
     * @param versionId
     * @param wordDetails
     * @param dbCallBack
     */
    public void addAsynIfNoExist(final String versionId, final List<WordDetail> wordDetails, final DBCallBack dbCallBack) {
        new DBAsyn<Boolean>() {
            @Override
            protected Boolean doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                try {
                    return  executeaddIfNoExistData(versionId, wordDetails, dbCallBack);
                } catch (Exception e) {
                    return false;
                }
            }

            @Override
            protected void onPostExecute(Boolean result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();


    }

    public void deleteAllAsny(final String versionId, final DBCallBack dbCallBack) {
        new DBAsyn<Boolean>() {
            @Override
            protected Boolean doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                try {
                    return executeDeleteAll(versionId);
                } catch (Exception e) {
                    return false;
                }
            }

            @Override
            protected void onPostExecute(Boolean result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();

    }

    public void queryAllAsny(final String versionId, final DBCallBack dbCallBack) {
        new DBAsyn<List<WordDetail>>() {
            @Override
            protected List<WordDetail> doInBackground() {
                CWLog.e(TAG, "执行数据库的线程" + Thread.currentThread().getName());
                return executeQueryAll(versionId);
            }

            @Override
            protected void onPostExecute(List<WordDetail> result) {
                dbCallBack.dbResult(result);
                CWLog.e(TAG, "获取数据库操作结果的线程");
            }
        }.execute();
    }
}
